package com.hotel_management_backstage.lin.service.staffManagement;

import com.hotel_management_backstage.lin.dao.SysUserRoleMapper;
import com.hotel_management_backstage.lin.dao.staffManagement.SysStaffDepartmentMapper;
import com.hotel_management_backstage.lin.entity.SysUser;
import com.hotel_management_backstage.lin.entity.SysUserRole;
import com.hotel_management_backstage.lin.entity.staffManagement.*;
import com.hotel_management_backstage.lin.service.SysRoomService;
import com.hotel_management_backstage.lin.service.SysUserRoleService;
import com.hotel_management_backstage.lin.service.SysUserService;
import com.hotel_management_backstage.lin.utils.security.JwtTokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class SysStaffDepartmentService {

    @Autowired
    private SysStaffDepartmentMapper sysStaffDepartmentMapper;

    @Autowired
    private SysStaffService sysStaffService;

    @Autowired
    private SysDepartmentService sysDepartmentService;

    @Autowired
    private SysStaffDepartmentService sysStaffDepartmentService;

    @Autowired
    private SysUserStaffService sysUserStaffService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    public SysStaffDepartment selectByStaffId(int staffId){
        return sysStaffDepartmentMapper.selectByStaffId(staffId);
    }

    public List<SysStaffShow> selectAllStaffShow(){
        List<SysStaffShow> sysStaffShows = new ArrayList<>();
        List<SysStaff> sysStaffs = sysStaffService.selectAllStaff();
        for(SysStaff sysStaff : sysStaffs){
            SysStaffDepartment sysStaffDepartment = sysStaffDepartmentMapper.selectByStaffId(sysStaff.getId());
            SysDepartment sysDepartment = sysDepartmentService.selectDepartmentById(sysStaffDepartment.getDepartmentId());
            SysUserStaff sysUserStaff = sysUserStaffService.selectByStaffId(sysStaff.getId());
            SysUserRole userRole = sysUserRoleService.selectByUserId(sysUserStaff.getUserId());
            SysUser sysUser = sysUserService.selectById(sysUserStaff.getUserId());
            SysStaffShow sysStaffShow = new SysStaffShow();
            sysStaffShow.setStaffNumber(sysStaff.getStaffNumber());
            sysStaffShow.setStaffName(sysStaff.getStaffName());
            sysStaffShow.setStaffTitle(sysStaff.getStaffTitle());
            sysStaffShow.setStaffEntryTime(sysStaff.getStaffEntryTime());
            sysStaffShow.setStaffSalary(sysStaff.getStaffSalary());
            sysStaffShow.setStaffBonus(sysStaff.getStaffBonus());
            sysStaffShow.setDepartmentName(sysDepartment.getDepartmentName());
            sysStaffShow.setRole(userRole.getRoleId());
            sysStaffShow.setUserName(sysUser.getName());
            sysStaffShow.setPassWord(sysUser.getPassword());
            sysStaffShows.add(sysStaffShow);
        }
        return sysStaffShows;
    }

    void insertSysStaffDepartment(SysStaffDepartment sysStaffDepartment){
        sysStaffDepartmentMapper.insertSysStaffDepartment(sysStaffDepartment);
    }

    @Transactional
    public void updateStaff(SysStaffShow sysStaffShow){
        SysStaff sysStaff = sysStaffService.selectByStaffNumber(sysStaffShow.getStaffNumber());
        sysStaff.setStaffNumber(sysStaffShow.getStaffNumber());
        sysStaff.setStaffName(sysStaffShow.getStaffName());
        sysStaff.setStaffTitle(sysStaffShow.getStaffTitle());
        sysStaff.setStaffEntryTime(sysStaffShow.getStaffEntryTime());
        sysStaff.setStaffSalary(sysStaffShow.getStaffSalary());
        sysStaff.setStaffBonus(sysStaffShow.getStaffBonus());
        sysStaffService.updateSysStaff(sysStaff);
        SysStaffDepartment sysStaffDepartment = sysStaffDepartmentService.selectByStaffId(sysStaff.getId());
        SysDepartment sysDepartment = sysDepartmentService.selectDepartmentById(sysStaffDepartment.getDepartmentId());
        sysDepartment.setDepartmentName(sysStaffShow.getDepartmentName());
        sysStaffDepartment.setDepartmentId(changeDepartmentFromNameToId(sysStaffShow.getDepartmentName()));
//        sysDepartmentService.updateSysDepartment(sysDepartment);
        sysStaffDepartmentMapper.updateSysStaffDepartmentByStaffId(sysStaffDepartment);
        SysUserStaff sysUserStaff = sysUserStaffService.selectByStaffId(sysStaff.getId());
        SysUser sysUser = sysUserService.selectById(sysUserStaff.getUserId());
        sysUser.setName(sysStaffShow.getUserName());
        sysUser.setPassword(sysStaffShow.getPassWord());
        sysUserService.updateUser(sysUser,sysStaffShow.getRole());
    }

    @Transactional
    public void insertStaff(SysStaffShow sysStaffShow){
        SysStaff sysStaff = new SysStaff();
        sysStaff.setStaffNumber(sysStaffShow.getStaffNumber());
        sysStaff.setStaffName(sysStaffShow.getStaffName());
        sysStaff.setStaffTitle(sysStaffShow.getStaffTitle());
        sysStaff.setStaffEntryTime(sysStaffShow.getStaffEntryTime());
        sysStaff.setStaffSalary(sysStaffShow.getStaffSalary());
        sysStaff.setStaffBonus(sysStaffShow.getStaffBonus());
        sysStaffService.insertSysStaff(sysStaff);
        int staffId = sysStaff.getId();
        SysDepartment sysDepartment = sysDepartmentService.selectSysDepartmentByDepartmentName(sysStaffShow.getDepartmentName());
        int departmentId = sysDepartment.getId();
        SysStaffDepartment sysStaffDepartment = new SysStaffDepartment();
        sysStaffDepartment.setStaffId(staffId);
        sysStaffDepartment.setDepartmentId(departmentId);
        sysStaffDepartmentService.insertSysStaffDepartment(sysStaffDepartment);
        SysUser sysUser = new SysUser();
        sysUser.setName(sysStaffShow.getUserName());
        sysUser.setPassword(sysStaffShow.getPassWord());
        sysUserService.insertUser(sysUser,sysStaffShow.getRole());
        Integer userId = sysUser.getId();
        sysUserRoleMapper.insertUserRole(userId,sysStaffShow.getRole());
        SysUserStaff sysUserStaff = new SysUserStaff();
        sysUserStaff.setUserId(userId);
        sysUserStaff.setStaffId(staffId);
        sysUserStaffService.insertSysUserStaff(sysUserStaff);
    }

    @Transactional
    public void deleteStaffByStaffNumber(int staffNumber){
        SysStaff sysStaff = sysStaffService.selectByStaffNumber(staffNumber);
        SysUserStaff sysUserStaff = sysUserStaffService.selectByStaffId(sysStaff.getId());
        SysUser sysUser = sysUserService.selectById(sysUserStaff.getUserId());
        sysStaffService.deleteSysStaffById(sysStaff.getId());
        sysUserStaffService.deleteSysUserStaffByUserId(sysUserStaff.getUserId());
        sysUserRoleService.deleteSysUserRoleByUserId(sysUserStaff.getUserId());
        sysUserService.deleteUserById(sysUser.getId());
    }

    private int changeDepartmentFromNameToId(String DepartmentName){
        if(Objects.equals(DepartmentName, "人事部")){
            return 1;
        }else if(Objects.equals(DepartmentName, "前厅部")){
            return 2;
        }else if(Objects.equals(DepartmentName, "客房部")){
            return 3;
        }else if(Objects.equals(DepartmentName, "餐饮部")){
            return 4;
        }else if(Objects.equals(DepartmentName, "娱乐部")){
            return 5;
        }else if(Objects.equals(DepartmentName, "保安部")){
            return 6;
        }
        return 0;
    }
}
